<?php
/**
 * 商城首页应用控制层基类，前台后首页都继承该类
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK_Action
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: Index.class.php 7632 2011-06-04 06:48:16Z ouyangxiong $
 * 
 */
class ThinkAPK_Action_Index extends ThinkAPK_Action_Web {
	
	/**
	 * 首页赋值
	 *
	 */
	public function globalAssignIndex(){
		$this->setKeywords(C('shop_keywords'));
    	$this->setDescription(C('shop_desc'));
    	$this->assign('playimg', D('RelationImg')->get('mainplay', 0));//主图片播放
    	$this->assign('notice', D('Article')->getNotice(8));//实惠公告
    	$this->assign('module_list', $this->getModule()); //首页模块
    	$this->assign('module_promote', D('RelationGoods')->get('scroll-promote')); //滚动促销
    	$this->assign('comment', D('Comment')->indexComment()); // 底部评论第一页
    	$this->assign('bbshot', D('RelationImg')->get('bbshot'));	// 论坛热贴自定义数据
    	$this->assign('goods_HList_one',D('RelationGoods')->get_limit('i-hot-box-one',3));
    	$this->assign('goods_HList_tow',D('RelationGoods')->get_limit('i-hot-box-tow',3));
    	$this->assign('goods_HList_thr',D('RelationGoods')->get_limit('i-hot-box-thr',3));
    	$this->assign('goods_HList_foe',D('RelationGoods')->get_limit('i-hot-box-foe',3));
    	$this->assign('fontShowimli',D('RelationText')->where("type = 'index-i-hot-midle'")->order('sort_order')->limit(4)->select());//中部文字
    	$this->assign('ImgShowbutm', D('RelationImg')->get('index-i-hot-midle'));  
    	$this->assign('saletop', $this->getTop10()); // 销售排行
    	$this->assign('todaypromote', $this->getPromote()); 
    	//module_promote
	}
	
	/**
	 * 首页模块数据
	 *
	 * @return array
	 */
	public function getModule(){
		$D = D('Module');
		//后台需要显示所有模块
		if(APP_NAME == 'admin'){
			$module = $D->getAllModule('index', true);
		}else {
			$module = $D->getAllModule('index');
		}
		foreach($module as $key=>$mod){
			//显示抬头
			if($mod['show_title'] && $mod['module_type'] == 'function_module'){
				//抬头分类链接
				$title_category = D('Category')->getCategoryByIds($mod['title_cates']);
				if($title_category)	$module[$key]['title_category'] = $title_category;
				//抬头滚动
				$title_scroll = D('RelationText')->get('module-top-scroll', $mod['id']);
				if($title_scroll) $module[$key]['title_scroll'] = $title_scroll;
				unset($title_category, $title_scroll);
			}
			//功能模块的数据
			if($mod['module_type'] == 'function_module'){
				//左边数据
				switch ($mod['left_type']){
					case 'swtich_goods':
						$module[$key]['left'] = D('RelationGoods')->get('left-module-goods', $mod['id']);
						break;
					case 'ad_link':
						$module[$key]['left'] = D('RelationImg')->get('left-module-ad', $mod['id']);
						break; 
					default:
						break;
				}
				//中间数据
				if($mod['center_type'] != '' && $mod['center_type'] != 'none'){
					$module[$key]['center'] = D('RelationImg')->get('center-module-ad', $mod['id']);
				}
				//右边数据
				switch ($mod['right_type']){
					case 'text_link':
						$module[$key]['right'] = D('RelationImg')->get('right-module-ad', $mod['id']);
						break;
					case 're_goods':
						$module[$key]['right'] = D('RelationGoods')->get('right-module-goods', $mod['id']);
						break;
					default:
						break;
				}
			}
			//图片广告模块
			if($mod['module_type'] == 'ad_module'){
				$module[$key]['ad'] = D('RelationImg')->get('module-ad', $mod['id']);
			}
			//自定义内容模块
			if($mod['module_type'] == 'custom_module'){
				$module[$key]['content'] = D('ModuleContent')->where(array('module_id'=>$mod['id']))->getField('content');
			}
			$module[$key]['color_type'] = empty($mod['color_type']) ? 'pink' : $mod['color_type'];
			unset($mod, $key);
		}
		unset($D);
		return $module;
	}
	
	/**
	 * 取得首页今日特价
	 * 
	 * @return array
	 */
	public function getPromote(){
		$tejia_type = C('index_tejia_type');
		//自定义
		if($tejia_type){
			$list = D('RelationGoods')->get('index-tejia');
		//系统自动读取
		}else{
			$type = C('index_tejia_data_type');
			$order = C('index_tejia_order');
			$number = C('index_tejia_number');
			$where = 'is_delete=0 AND is_on_sale=1 ';
			$orderby = '';
			$time = time();
			if($type == '0'){
				$where .= ' AND is_promote=1 AND promote_start_date<='.$time.' AND promote_end_date>='.$time;
			}elseif($type == '1'){
				$where .= ' AND is_promote=1 AND ((promote_start_date<='.$time.' AND promote_end_date>='.$time.') OR promote_start_date>'.$time.') ';
			}elseif($type == '2'){
				$orderby = ' rand() ';
			}
			if(empty($orderby)){
				if($order == '0'){
					$orderby = ' promote_start_date DESC ';
				}elseif($order == '1'){
					$orderby = ' update_time DESC ';
				}else{
					$orderby = ' add_time DESC ';
				}
			}
			$limit = ($number > 0) ? $number : 3;
			$list = D('Goods')->where($where)->field('goods_id as id, click_count, goods_name as title, goods_thumb, goods_maidian as maidian, is_promote, promote_start_date, promote_end_date, promote_price')
				->limit($limit)
				->order($orderby)
				->findAll();
			if($list){
				foreach($list as $key=>$goods){
					$price = 0;
					if($goods['is_promote']){
						$price = D('Goods')->bargainPrice($goods['promote_price'], $goods['promote_start_date'], $goods['promote_end_date']);
					}
					if($price == 0) $price = $goods['shop_price']; 
					$price = min(array($price, $goods['shop_price']));
					$list[$key]['price'] = $price;
					$list[$key]['thumb'] = $this->getImagePath($goods['goods_thumb']);
					$list[$key]['url'] = U('mall://Product/index', array('id'=>$goods['id']));
					$list[$key]['format_price'] = $this->formatPrice($price);
					$list[$key]['name'] = $goods['goods_name'];
					//自动读取时的商品原价
					$list[$key]['format_market_price'] = $this->formatPrice($goods['shop_price']);
				}
			}
		}
		return $list;
	}
	
	/**
	 * 取得首页销售排行
	 * 
	 * @return array
	 *
	 */
	public function getTop10(){
		return D('SalesTop')->get('index');
	}
	
	/**
	 * 首页滚动促销
	 *
	 * @return array
	 */
	public function scrollPromote(){
		return D('RelationGoods')->get('scroll-promote');
	}
}
?>